
clear
clear all
clear mata
clear matrix
set more off
set matsize 11000
set maxvar 30000
cap log off
capture log close
set emptycells drop
pause on


***********************************************
* USER DEFINE FILEPATH FOR REPLICATION FOLDER *
***********************************************

global path "C:\Users\wb520443\Dropbox\Research Projects\Global pollution\2_analysis\Replication"



***********************************************


global opts		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))  ) star(* .10 ** .05 *** .01) noobs nocons

global opts_fs		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))) star(* .10 ** .05 *** .01) noobs nocons


global input "$path\data"
global figdat "$path\figdat"
global figures "$path\figures"
global tables "$path\tables"
		
	use "$input//rwi_pollution_analysis_collapsed.dta", replace
	
	encode country, g(country_code)
	
	replace error=1/error
	g both=population*error
	g both2=country_population*error
	
	g low=.
	g high=.
	forvalues i=1(1)103{
		
		sum rwi if country_code==`i', d
			local l=`r(p5)'
			local h=`r(p95)'
			replace low=1 if rwi<=`l'  & country_code==`i'
			replace high=1 if rwi>=`h'  & country_code==`i'
			
	}
	
	
	g rwi_pool=rwi
	
	winsor rwi, p(0.05) g(rwi2)
		rename rwi2 rwi_all
		
	forvalues i=1(1)103{
		sum rwi if country_code==`i', d
			replace rwi=`r(p5)' if rwi<`r(p5)' & country_code==`i'
			replace rwi=`r(p95)' if rwi>`r(p95)' & country_code==`i'
	}	
	
	label var rwi_all "Wealth Index"
	label var rwi "Wealth Index"
	
	rename (rwi_all rwi) (rwi rwi2)	
	reghdfe mean_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t1c1
	rename (rwi rwi2) (rwi_all rwi) 	
	reghdfe mean_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t1c2	
	rename (rwi_pool rwi) (rwi rwi2)
	reghdfe mean_pm25 rwi [pweight=both2] if high==1|low==1, absorb(i.country_code)
		est store t1c3
	reghdfe mean_pm25 rwi [pweight=both2] if low==1, absorb(i.country_code)
		est store t1c4
	reghdfe mean_pm25 rwi [pweight=both2] if high==1, absorb(i.country_code)
		est store t1c5
	
	rename (rwi2 rwi) (rwi rwi_pool)
	rename (rwi_all rwi) (rwi rwi2)	
	reghdfe median_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t2c1
	rename (rwi rwi2) (rwi_all rwi)	
	reghdfe median_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t2c2	
	rename (rwi_pool rwi) (rwi rwi2)
	reghdfe median_pm25 rwi [pweight=both2] if high==1|low==1, absorb(i.country_code)
		est store t2c3
	reghdfe median_pm25 rwi [pweight=both2] if low==1, absorb(i.country_code)
		est store t2c4
	reghdfe median_pm25 rwi [pweight=both2] if high==1, absorb(i.country_code)
		est store t2c5
		
	rename (rwi2 rwi) (rwi rwi_pool)	
	rename (rwi_all rwi) (rwi rwi2)	
	reghdfe max_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t3c1
	rename (rwi rwi2) (rwi_all rwi)	
	reghdfe max_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t3c2		
	rename (rwi_pool rwi) (rwi rwi2)
	reghdfe max_pm25 rwi [pweight=both2] if high==1|low==1, absorb(i.country_code)
		est store t3c3
	reghdfe max_pm25 rwi [pweight=both2] if low==1, absorb(i.country_code)
		est store t3c4
	reghdfe max_pm25 rwi [pweight=both2] if high==1, absorb(i.country_code)
		est store t3c5
		
	rename (rwi2 rwi) (rwi rwi_pool)
	
	file open  t	using "$tables/table_winsored.tex", replace write
		file write t	"\begin{table}[htbp] \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
					"\caption{Correlation between RWI and PM\$_{2.5}\$, winsored}\label{tab:table_winsored}" _n  ///
					"\begin{tabular*}{1.4\textwidth}{@{\extracolsep{\fill}}l*{8}{c}}" _n "\midrule" _n ///
					"&\multicolumn{2}{c}{}&\multicolumn{3}{c}{Top \& bottom 5\% sample}\\" _n "\cmidrule{4-6}" _n ///
					"&Winsored across&Winsored within&Pooled&Bottom 5\%&Top 5\%\\" _n ///
					"\midrule" _n 
		file close t
		
			
		file open  t 	using "$tables/table_winsored.tex", append write
		file write t 	"(A) PM\$_{2.5}\$ measured as average\\" _n "\cmidrule{1-1}" _n
		file close t
		
		esttab t1* using "$tables/table_winsored.tex", l keep(rwi*) ///
			s(N, l("N") f(%11.0fc) lay(@)) $opts  
			
		file open  t 	using "$tables/table_winsored.tex", append write
		file write t 	"(B) PM\$_{2.5}\$ measured as median\\" _n "\cmidrule{1-1}" _n
		file close t
		
		esttab t2* using "$tables/table_winsored.tex", l keep(rwi*) ///
			s(N, l("N") f(%11.0fc) lay(@)) $opts 	
			
		file open  t 	using "$tables/table_winsored.tex", append write
		file write t 	"(C) PM\$_{2.5}\$ measured as average during maximum month\\" _n "\cmidrule{1-1}" _n
		file close t
		
		esttab t3* using "$tables/table_winsored.tex", l keep(rwi*) ///
			s(N, l("N") f(%11.0fc) lay(@)) $opts 	
		
		file open  t 	using "$tables/table_winsored.tex", append write
		file write t "\\" ///
						"\textbf{Fixed Effects:}&	&	&	&	&	\\" ///
						"County 				&Y	&Y	&Y	&Y	&Y	 	\\" ///
						"\midrule" _n "\end{tabular*}" _n ///
						"\begin{tabular*}{1.4\textwidth}{p{8.8in}}" _n ///
						"\footnotesize \textsc{Notes:} Each column reports the results of a linear fixed effects regression of pollution against RWI at the gridcell level across all countries in our sample. Each row is a seperate regression. RWI is a country specific wealth index that ranges across our full dataset from -2.022 to 2.456. We collapse pollution across all months and years in the sample by RWI gridcell. In row \textbf{A} we collapse as the average across all months and years. In row \textbf{B} we collapse as the median. In row \textbf{C} we calculate the monthly average across all years and assign gridcells the average in the month with the highest monthly average. We winsorize at the fifth and ninety-fifth percentiles of the RWI distribution across the whole sample in the Winsored across column and at the same thresholds within countries in the Winsored within column. In columns 3-5 we calculate the top and bottom 5\% of RWI points within each country and only include the points that fall into the categories indicated in the column headings. We weight all regressions by the inverse of the error reported in the RWI data multiplied by the population containing the grid point. \textit{p}-values reported in brackets. (* p$<$.10 ** p$<$.05 *** p$<$.01)." ///
						"\end{tabular*}" _n "\end{table}" _n 
		file close t
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	